package com.ts.manager;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.rio.core.U;
import com.rio.helper.file.FileCache;
import com.rio.helper.file.FileHelper;
import com.rio.helper.json.G;
import com.ts.host.APP;

/**
 * 切记都是耗时操作
 * @author mrnat
 *
 */
/**
 * @author mrnat
 *
 */
/**
 * @author mrnat
 *
 */
public class CacheHelper {
	
	public static final int TIME_HOUR = 60 * 60;
	public static final int TIME_DAY = TIME_HOUR * 24;
	private static final int MAX_SIZE = 1000 * 1000 * 50; // 50 mb
	private static final int MAX_COUNT = Integer.MAX_VALUE; // 不限制存放数据的数量
	private ConcurrentHashMap<String, FileCache> mInstanceMap;

	private  static CacheHelper mInstance;
	
	public static CacheHelper getInstance(){
		if(mInstance == null){
			mInstance = new CacheHelper();
		}
		return mInstance;
		
	}
	private Map<String, FileCache> getMap(){
		if(U.isNull(mInstanceMap)){
			mInstanceMap = new ConcurrentHashMap<String, FileCache>();
		}
		return mInstanceMap;
	}
	/**
	 * @param cacheName
	 * @param max_zise
	 * @param max_count
	 */
	public FileCache get(String cacheName, long max_size, int max_count){
		FileCache manager = getMap().get(cacheName);
		if (manager == null) {
			manager = new FileCache(getFile(cacheName,max_size), max_size, max_count);
			getMap().put(cacheName, manager);
		}
		return manager;	
	}
	
	public FileCache get(String cacheName){
		return get(cacheName, MAX_SIZE, MAX_COUNT);
	}
	
	public FileCache get(String cacheName,int max_count){
		return get(cacheName, MAX_SIZE, max_count);
	}

	/**
	 * 先检查SD卡在不在
	 * SD卡够不够空间
	 * 不够放在 /data/xxxx/cache/
	 * @param cacheName
	 * @param max_size
	 * @return
	 */
	private File getFile(String cacheName,long max_size){
		File f =null;
		if(FileHelper.getSDCardAvailableStorageSize()> max_size){
			f = FileHelper.getSDCardFile(cacheName);
		}
		if(U.isNull(f))
			f = new File(APP.getContext().getCacheDir(), cacheName);
		return f;
	}
	
	/**
	 * 重置缓存参数
	 * @param cacheName
	 * @param max_size
	 * @param max_count
	 */
	public void mkCache(String cacheName, long max_size, int max_count){
		FileCache manager = getMap().get(cacheName);
		if(U.notNull(manager)){
			if(!manager.check(max_size, max_count)){
				manager.clear();
				getMap().remove(cacheName);
				manager = new FileCache(getFile(cacheName,max_size), max_size, max_count);
				getMap().put(cacheName, manager);
			}
		}else{
			manager = new FileCache(getFile(cacheName,max_size), max_size, max_count);
			getMap().put(cacheName, manager);
		}
		
	}
	
	/**
	 * 读取某个缓存文件
	 * @param cacheName 缓存目录
	 * @param key	缓存文件
	 * @param cls	转化的类
	 * @return
	 */
	public <T> T getCache(String cacheName,String key ,Class<T> cls){
		FileCache cache = get(cacheName);
		if(U.notNull(cache)){
			String json = cache.getAsString(key);
			return G.toObject(json, cls);
		}
		return null;		
	}
	
	public <T> T getCache(String key ,Class<T> cls){
		return getCache(U.getName(cls), key, cls);		
	}
	
	/**
	 * 写入某个缓存文件
	 * @param cacheName 缓存目录
	 * @param key		缓存文件
	 * @param result	缓存对象
	 * @param saveTime	缓存时长
	 * @return
	 */
	public <T> boolean putCache(String cacheName,String key,T result,int saveTime){
		FileCache cache = get(cacheName);
		if(U.notNull(cache)){
			String json = G.toJson(result);
			if(U.notNull(json)){
				if(saveTime > 0){
					cache.put(key, json,saveTime);
				}else{
					cache.put(key, json);
				}
				return true;
			}
		}
		return false;
	} 
	
	public <T> boolean putCache(String key,T result,int saveTime){
		return putCache(U.getName(result.getClass()),key,result,saveTime);
	} 
	
	public <T> boolean putCache(String key,T result){
		return putCache(key,result,0);
	} 
}
